<template>
<!-- 登录模块 -->
  <el-row type="flex" class="row-bg" justify="center">
    <el-col :xl="6" :lg="7">
      <h2>欢迎使用地方政府人员管理系统</h2>
      <el-image style="height: 60%; width: 80%" :src="require('@/assets/img.png')"></el-image>

    </el-col>
    <el-col :span="1">
      <el-divider direction="vertical"></el-divider>
    </el-col>
    <el-col :xl="6" :lg="7">
      <el-form @keyup.native.enter="submitForm('loginForm')" :model="loginForm" :rules="rules" ref="loginForm"
               label-width="80px" class="demo-loginForm"
      >
        <el-form-item label="用户名" prop="username" style="width: 380px">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" style="width: 380px" >
          <el-input type="password" v-model="loginForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="captchaImg" style="width: 380px">
          <el-input v-model="loginForm.captchaImg" style="width: 172px; float: left"></el-input>
          <el-image :src="captchaImg" class="captchaImg" @click="getCaptchaImg"></el-image>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
export default {
  name: 'Login',
  //页面数据
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        captchaImg: '', //验证码
        codeKey: '' //验证码的key 存在redis中
      },
      captchaImg: '', //验证码
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        captchaImg: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 5, max: 5, message: '长度为 5 个字符', trigger: 'blur' }
        ]
      },
    }
  },
  //页面加载的时候会调用这里面的方法。 vue
  created() {
    this.getCaptchaImg()
    // this.submitForm('loginForm')
  },
  //页面的方法区
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //发起登录请求，
          this.axios.post('/login', this.loginForm).then(resp => {
            if (resp.data.token) {
              this.$store.commit('user/SET_TOKEN', resp.data.tokenHead + resp.data.token)
              // replace 不可以回到上一页，push 可以回到上一页
              this.$router.replace('/index')
            } else {
              this.getCaptchaImg()
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    //获取验证码
    getCaptchaImg() {
      //发起一个http请求。请求后端的一个接口 get post
      this.axios.get('/captcha').then(resp => {
        this.loginForm.codeKey = resp.data.key
        this.captchaImg = resp.data.captchaImg
      })
    }
  }
}
</script>

<style scoped>
.el-row {
  background-color: #FAFAFA;
  height: 100%;
  margin: 0;
  padding: 0px;
  /*width: 100%;*/
  display: flex;
  align-items: center;
  text-align: center;
  display: flex;
  flex-wrap: wrap;
}

.div {

}

.el-divider {
  height: 200px;
}

.captchaImg {
  float: left;
  margin-left: 8px;
  border-radius: 4px;
  height: 40px;
  width: 120px;
}

</style>
